package com.jellyfishumbrella.GL;

import static org.lwjgl.opengl.GL11.*;

import java.util.ArrayList;
import java.util.Map;

import megamu.mesh.Region;

import org.lwjgl.BufferUtils;
import org.lwjgl.util.vector.Vector3f;

import be.humphreys.simplevoronoi.Cell;

import com.jellyfishumbrella.Corner;
import com.jellyfishumbrella.GameLogic;
import com.jellyfishumbrella.LandscapeData;
import com.jellyfishumbrella.Vertex;

public class LandscapeGLMesh extends MeshGL {
	public float			maxAlt		= 1.0f;
	public boolean			isActive	= false;
	public boolean			isReady		= false;

	public LandscapeData	landscapeData;

	public LandscapeGLMesh(LandscapeData landscapeData) {
		this.landscapeData = landscapeData;
		maxAlt = (float) landscapeData.maxAlt;
		initVertexData();
		setupVertOrderBuffer();
		
	}

	public void render() {
		float NORMAL_SCALE = 100.0f;
		boolean doFill = false;
		glPushMatrix();
		glTranslatef(pos.x, pos.y, pos.z);
		glShadeModel(GL_SMOOTH);

		Vector3f currCoord;

		Region[] regions = landscapeData.voronoi.getRegions();
		for (int i = 0; i < regions.length; i++) {

			ArrayList<Vertex> regionVerts = regions[i].getVerts();
			Vector3f normalGL = regions[i].site.normal.glCoords();

			// glBegin(GL_LINE_LOOP);
			glBegin(GL_TRIANGLE_FAN);

			currCoord = regions[i].site.glCoords();

			for (int j = 0; j < regionVerts.size(); j++) {
				currCoord = regionVerts.get(j).glCoords();

				// if (currCoord.y == 0) {
				// if(regions[i].site.isBorder){
				if (regions[i].site.isWater) {
					glColor3f(0.0f, 0.0f, 1.0f);
				} else {
					glColor3f(1.0f, 1.0f, 1.0f);
				}

				normalGL = regionVerts.get(j).normal.glCoords();
				glNormal3f(normalGL.x, normalGL.y, normalGL.z);
				glVertex3f(currCoord.x, currCoord.y, currCoord.z);
			}

			currCoord = regionVerts.get(1).glCoords();
			glNormal3f(normalGL.x, normalGL.y, normalGL.z);
			glVertex3f(currCoord.x, currCoord.y, currCoord.z);

			glEnd();
		}
	}

	public void initVertexData() {
		for (int i = 0; i < NUM_VBOS; i++) {
			if (vertexData[i] == null || vertexOrder[i] == null) {
				vertexData[i] = BufferUtils.createFloatBuffer(landscapeData.mergedVerts.size() * (3 + 3));
				vertexOrder[i] = BufferUtils.createShortBuffer(landscapeData.mergedVerts.size() * (3 + 3));
			} else {
				vertexData[i] = null;
				vertexOrder[i] = null;
			}
		}

		Vector3f currCoord;
		Vector3f currNormal;
		Region[] regions = landscapeData.voronoi.getRegions();
		short glID = 0;
		for (Map.Entry<String, Vertex> entry : landscapeData.mergedVerts.entrySet()) {
			Vertex currVert = (Vertex) entry.getValue();
			currCoord = currVert.glCoords();
			currNormal = currVert.normal.glCoords();

			currVert.glID = glID++;
			vertexData[0].put(currCoord.x);
			vertexData[0].put(currCoord.y);
			vertexData[0].put(currCoord.z);

			vertexData[0].put(currNormal.x);
			vertexData[0].put(currNormal.y);
			vertexData[0].put(currNormal.z);
		}
		vertexData[0].flip();
		setupVertOrderBuffer();
		setupMaterialsAndShaders();
		initGPU();
	}

	public void setupVertOrderBuffer() {
		Vector3f currCoord;

		Region[] regions = landscapeData.voronoi.getRegions();
		for (int i = 0; i < regions.length; i++) {
			ArrayList<Vertex> regionVerts = regions[i].getVerts();
			Vector3f normalGL = regions[i].site.normal.glCoords();
			
			for (int j = 0; j < regionVerts.size(); j++) {
				Vertex currVert = regionVerts.get(j);

				vertexOrder[0].put(regionVerts.get(j).glID);
			}
			vertexOrder[0].put(regionVerts.get(1).glID);
			vertexOrder[0].put(Short.MAX_VALUE);
		}
		vertexOrder[0].flip();
	}

	private void setupMaterialsAndShaders() {
		float[] amb = { 0.0f, 0.0f, 0.0f, 1.0f };
		float[] diff = { 1.0f, 1.0f, 1.0f, 1.0f };
		float[] spec = { 1.0f, 1.0f, 1.0f, 1.0f };
		float gloss = 60.0f;

		materials = new Material[NUM_VBOS];
		shaderFiles = new String[NUM_VBOS][2];

		for (int i = 0; i < NUM_VBOS; i++) {
			materials[i] = new Material(amb, diff, spec, gloss);
			shaderFiles[i][0] = "src/com/jellyfishumbrella/GL/Shaders/starter.vert";
			shaderFiles[i][1] = "src/com/jellyfishumbrella/GL/Shaders/starter.frag";
		}
	}
}
